package com.dianrui.qiyouriding.util;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonUtils {

    /**
     * <p>Title: </p>
     * <p>Description: cannot be instantiated</p>
     */
    public JsonUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static Map<String, Object> getMap(String jsonString) {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonString);
            @SuppressWarnings("unchecked")
            Iterator<String> keyIter = jsonObject.keys();
            String key;
            Object value;
            Map<String, Object> valueMap = new HashMap<String, Object>();
            while (keyIter.hasNext()) {
                key = (String) keyIter.next();
                value = jsonObject.get(key);
                valueMap.put(key, value);
            }
            return valueMap;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }


    /**
     * @Description: 生成json字符串
     * list转json
     */
    public static <T> String listToJson(List<T> list) {
        try {
            if (null == list || list.isEmpty()) {
                return null;
            }
            Gson gson = new Gson();
            return gson.toJson(list);
        } catch (Exception e) {}
        return null;
    }

    /**
     *
     * java对象转换成json
     */
    public static String javaBeanToJson(Object object) {
        try {
            Gson gson = new Gson();
            return gson.toJson(object);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 取出json字符串中指定内容
     */
    public static String getJsonStr(String JsonStr, String name) {
        if (TextUtils.isEmpty(JsonStr) || TextUtils.isEmpty(name)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(JsonStr);
            if (jsonObject != null && jsonObject.has(name))
                return jsonObject.getString(name);
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    /**
     * @Title: jsonStrToBean
     * @Description: json解析成java bean可用于多层解析
     */
    public static <T> T jsonStrToBean(String jsonStr, Class<T> c) {
        try {
            Gson gson = new Gson();
            return gson.fromJson(jsonStr, c);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Title: jsonStrToList
     * @Description: 将json数组解析成list [{},{},{}]
     *
    List<RoleListBean> list = JsonUtils.jsonStrToList(rolelist,
    new TypeToken<ArrayList<RoleListBean>>() {
    }.getType());

     */
    public static <T> List<T> jsonStrToList(String jsonStr, Type type) {
        try {
            Gson gson = new Gson();
            return gson.fromJson(jsonStr, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Title: jsonStrToList
     * @Description: 将json数组解析成list [{},{},{}]
     * @return List<T> 返回类型
     * @throws
     */
    public static <T> List<T> jsonStrToList(String jsonStr, Class<T> c) {
        try {
            Gson gson = new Gson();
            Type objectType = new TypeToken<List<T>>() {}.getType();
            return gson.fromJson(jsonStr, objectType);
        } catch (Exception e) {}
        return null;
    }
}
